<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        /*  几个常用字符串hash算法的 hash.js*/

        function additive(key, prime) {
            key = key instanceof Buffer ? key : new Buffer(key);
            prime = prime == undefined ? 0xffffffff : prime;
            for (var hash = key.length, i = 0; i < key.length; i++) {
                hash += key[i];
            }
            return (hash % prime);
        }

        function rotating(key) {
            key = key instanceof Buffer ? key : new Buffer(key);
            for (var hash = key.length, i = 0; i < key.length; ++i) {
                hash = (hash << 4) ^ (hash >> 28) ^ key[i];
            }
            return hash;
        }

        function bernstein(key, prime) {
            key = key instanceof Buffer ? key : new Buffer(key);
            prime = prime == undefined ? 0xffffffff : prime;
            var hash = 0;
            for (i = 0; i < key.length; ++i) {
                hash = (hash << 5 + hash) + key[i];
            }
            return hash;
        }

        function fnv(key) {
            key = key instanceof Buffer ? key : new Buffer(key);
            var p = 16777619, hash = 0x811C9DC5;
            for (var i = 0; i < key.length; i++) {
                hash = (hash * p) ^ key[i];
            }
            hash += hash << 13;
            hash ^= hash >> 7;
            hash += hash << 3;
            hash ^= hash >> 17;
            hash += hash << 5;
            return hash;
        }

        function fnv1a(key) {
            key = key instanceof Buffer ? key : new Buffer(key);
            var p = 16777619, hash = 0x811C9DC5;
            for (var i = 0; i < key.length; i++) {
                hash = (hash ^ key[i]) * p;
            }
            hash += hash << 13;
            hash ^= hash >> 7;
            hash += hash << 3;
            hash ^= hash >> 17;
            hash += hash << 5;
            return hash;
        }

        hashCode = function(str){
            var hash = 0;
            if (str.length == 0) return hash;
            for (i = 0; i < str.length; i++) {
                char = str.charCodeAt(i);
                hash = ((hash<<5)-hash)+char;
                hash = hash & hash; // Convert to 32bit integer
            }
            return hash;
        }



        function djb2Code(str){
            var hash = 5381;
            for (i = 0; i < str.length; i++) {
                char = str.charCodeAt(i);
                hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
            }
            return hash;
        }


        function sdbmCode(str){
            var hash = 0;
            for (i = 0; i < str.length; i++) {
                char = str.charCodeAt(i);
                hash = char + (hash << 6) + (hash << 16) - hash;
            }
            return hash;
        }


        function loseCode(str){
            var hash = 0;
            for (i = 0; i < str.length; i++) {
                char = str.charCodeAt(i);
                hash += char;
            }
            return hash;
        }

        var s1={
            name:"秦有宝",
            age:12,
            good:true,
            hobby:{
                basketball:true,
            },
            dd:[{
                age:12,
                good:true,
            }]
        }
        var s2={
            name:"秦有宝",
            age:12,
            good:true,
            hobby:{
                basketball:true,
            },
            dd:[{
                age:12,
                good:true,
            }]
        }
        var s3={
            name:"王春霆",
            age:12,
            good:true,
            hobby:{
                basketball:true,
            },
            dd:[{
                age:12,
                good:true,
            }]
        }
        console.log(djb2Code(JSON.stringify(s1)))
        console.log(djb2Code(JSON.stringify(s2)))
        console.log(djb2Code(JSON.stringify(s3)))
        console.log(djb2Code(JSON.stringify(s1)) == djb2Code(JSON.stringify(s2)))
        console.log(djb2Code(JSON.stringify(s1)) == djb2Code(JSON.stringify(s3)))

    </script>
</head>
<body>

</body>
</html>